1
/****************************** Module Header ******************************\
2 * Module Name: WebBrowserEx.cs
3 * Project: CSWebBrowserSuppressError
4 * Copyright (c) Microsoft Corporation.
6 * This WebBrowserEx class inherits WebBrowser class and supplies following
8 * 1. Disable JIT Debugger.
9 * 2. Suppress html element errors of document loaded in this browser.
10 * 3. Handle navigation error.
12 * The class WebBrowser itself also has a Property ScriptErrorsSuppressed to hides
13 * all its dialog boxes that originate from the underlying ActiveX control, not
16 * This source is subject to the Microsoft Public License.
17 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
18 * All other rights reserved.
20 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
23 \***************************************************************************/
26 using System
.Security
.Permissions
;
27 using System
.Windows
.Forms
;
28 using Microsoft
.Win32
;
31 namespace CSWebBrowserSuppressError
33 public partial class WebBrowserEx
: WebBrowser
36 /// Get or Set whether JIT debugger needs to be disabled. You have to restart the
37 /// browser to take effect.
39 public static bool JITDebuggerDisabled
43 using (RegistryKey ieMainKey
= Registry
.CurrentUser
.OpenSubKey(
44 @"Software\Microsoft\Internet Explorer\Main"))
46 string keyvalue
= ieMainKey
.GetValue("Disable Script Debugger") as string;
47 return string.Equals(keyvalue
, "yes", StringComparison
.OrdinalIgnoreCase
);
52 var newValue
= value ? "yes" : "no";
54 using (RegistryKey ieMainKey
= Registry
.CurrentUser
.OpenSubKey(
55 @"Software\Microsoft\Internet Explorer\Main", true))
57 string keyvalue
= ieMainKey
.GetValue("Disable Script Debugger") as string;
58 if (!keyvalue
.Equals(newValue
, StringComparison
.OrdinalIgnoreCase
))
60 ieMainKey
.SetValue("Disable Script Debugger", newValue
);
66 // Suppress html element errors.
67 public bool HtmlElementErrorsSuppressed { get; set; }
69 AxHost
.ConnectionPointCookie cookie
;
71 WebBrowser2EventHelper helper
;
73 public event EventHandler
<WebBrowserNavigateErrorEventArgs
> NavigateError
;
75 [PermissionSetAttribute(SecurityAction
.LinkDemand
, Name
= "FullTrust")]
81 /// Register the Document.Window.Error event.
83 [PermissionSetAttribute(SecurityAction
.LinkDemand
, Name
= "FullTrust")]
84 protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e
)
86 base.OnDocumentCompleted(e
);
88 // Occurs when script running inside of the window encounters a run-time error.
89 this.Document
.Window
.Error
+= new HtmlElementErrorEventHandler(Window_Error
);
94 /// Handle html element errors of document loaded in this browser.
95 /// If HtmlElementErrorsSuppressed is set to true, then set the
96 /// Handled flag to true sothat browser will not display this error.
98 protected void Window_Error(object sender
, HtmlElementErrorEventArgs e
)
100 if (HtmlElementErrorsSuppressed
)
108 /// Associates the underlying ActiveX control with a client that can
109 /// handle control events including NavigateError event.
111 [PermissionSetAttribute(SecurityAction
.LinkDemand
, Name
= "FullTrust")]
112 protected override void CreateSink()
116 helper
= new WebBrowser2EventHelper(this);
117 cookie
= new AxHost
.ConnectionPointCookie(
118 this.ActiveXInstance
, helper
, typeof(DWebBrowserEvents2
));
123 /// Releases the event-handling client attached in the CreateSink method
124 /// from the underlying ActiveX control
126 [PermissionSetAttribute(SecurityAction
.LinkDemand
, Name
= "FullTrust")]
127 protected override void DetachSink()
139 /// Raises the NavigateError event.
141 protected virtual void OnNavigateError(WebBrowserNavigateErrorEventArgs e
)
143 if (this.NavigateError
!= null)
145 this.NavigateError(this, e
);